package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.redis.service.IuserInfoRedisService;
import cn.wolfcode.wolf2w.cache.redis.uitl.RedisKeys;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.uitl.NoNullStringUitl;
import cn.wolfcode.wolf2w.uitl.TelVerifyNuil;
import cn.wolfcode.wolf2w.uitl.newException.LogiclException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IuserInfoRedisService iuserInfoRedisService;
    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        userInfoRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> lists() {
        return userInfoRepository.findAll();
    }

    @Override
    public boolean findByPhone(String phone) {
        //2. 判断手机号是否符合格式
        TelVerifyNuil.isMobile(phone);
        UserInfo userInfo=userInfoRepository.findByPhone(phone);
        return userInfo !=null;
    }

    @Override
    public void sendVerifyCode(String phone,String url,String appkey) {
        //创建验证码
        String substring = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //拼接短信
        StringBuilder sb =new StringBuilder();
        sb.append("请在").append(RedisKeys.USER_KEY.getTime()).append("秒内使用").append(substring).append("验证码,否则无效");
        //假设发送成功
//        RestTemplate restTemplate =new RestTemplate();
//        //发送uil
//         * 定义了什么类型就返回一个什么类型的值,参数直接传入
//        String uils = restTemplate.getForObject(url, String.class,phone,"【凯信通】您的验证码是："+sb.toString(),appkey);
//            if (!uils.contains("Success")){
//                throw new LogiclException("短信发送失败");
//            }
        System.out.println(sb);
 //       System.out.println(uils);
        //把数据添加到redis中
        iuserInfoRedisService.save(phone,substring);
    }
    //处理注册信息
    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判断所有数是否为空,如果为空就抛出异常
        NoNullStringUitl.getNull(phone,"手机号码不能为空");
        NoNullStringUitl.getNull(nickname,"名字不能为空");
        NoNullStringUitl.getNull(password,"密码不能为空");
        NoNullStringUitl.getNull(rpassword,"确定密码不能为空");
        NoNullStringUitl.getNull(verifyCode,"验证码不能为空");
        //1. 判断2次输入的密码是否一致,利用自定义工具类
        NoNullStringUitl.isEquals(password,rpassword,"密码不一致");

        //2. 判断手机号是否符合格式
        TelVerifyNuil.isMobile(phone);
            //后写
        //3. 判断手机号是否唯一,通过手机查询
        //UserInfo userInfo = userInfoRepository.findByPhone(phone);
            boolean byPhone = this.findByPhone(phone);//调用自己类中的方法
        if (byPhone){
            throw  new LogiclException("手机已经被注册啦");
        }

        //4. 判断验证码是正确
        //拿到redis中验证码和传入的对比
        String code=iuserInfoRedisService.getCode(phone);
        if (!NoNullStringUitl.nullGetString(code)||!code.equals(verifyCode)){
            throw new LogiclException("验证码不正确");
        }
        //5. 注册
        UserInfo userInfo=new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setCity("");
        userInfo.setPassword(password);
        userInfo.setNickname(nickname);
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoRepository.save(userInfo);
 }

    /**
     * 处理登陆业务
     * @param username
     * @param password
     * @return
     */
    @Override
    public UserInfo logins(String username, String password) {
        //通过号码查询对象
        NoNullStringUitl.getNull(username,"账户不能为空");
        NoNullStringUitl.getNull(password,"密码不能为空");
        UserInfo user = userInfoRepository.findByPhone(username);
        if (user == null ){
            throw new LogiclException("账户不存在");
        }
        if (!password.equals(user.getPassword())){
            throw new LogiclException("密码不正确,请重新输入");
        }

        return user;
    }

    /**
     * 处理账户业务
     * @param user
     * @return
     */
    @Override
    public String setToken(UserInfo user) {
        //设置唯一的key值
        String token = UUID.randomUUID().toString().replace("-", "");
        //把对象放在redis中,拿到一个有前置的key
        iuserInfoRedisService.setToken(token, user);
        return token;
    }

    /**
     * 通过令牌,当做key拿到redis中带有的值
     * @param
     * @return
     */
    @Override
    public UserInfo getKeyUser(String token) {
        return iuserInfoRedisService.getKeyUser(token);
    }

    @Override
    public List<UserInfo> queryfindByCity(String keyword) {
        return iuserInfoRedisService.findByCity(keyword);
    }
}
